<?php
    include('time_config.php');
    include('mysql_config.php');

    $pdo = new PDO($dsn,$user,$password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $stmt = $pdo->prepare('select * from jdw_order where status in("1","2")');
    $stmt->execute();

    while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
        if(time()-$row['create_time'] > $order_time){
        //平台回扣
        $system_balance_sql = "select value from jdw_config where id = 7";
        $system_balance_result = $pdo->query($system_balance_sql);
        $system_balance = $system_balance_result->fetch(PDO::FETCH_ASSOC);
        $system_balance = $system_balance['value'];

        if(!empty($system_balance)){
            //平台回扣
            $system_balance_money = ($row['total_price']*($system_balance/100));
            $system_balance_money = substr(sprintf("%.3f",$system_balance_money),0,-1);
            $row['total_price'] = $row['total_price'] - $system_balance_money;

            $system_data['order_id'] = $row['id'];
            $system_data['system_rule'] = $system_balance;
            $system_data['money'] = $system_balance_money;
            //平台记录
            $system_balance_sql = "insert into jdw_system_balance (order_id,system_rule,money) values ('{$system_data['order_id']}','{$system_data['system_rule']}','{$system_data['money']}')";
            $pdo->query($system_balance_sql);
        }

        //上一级会员获得佣金
        $parent_id_sql = "select parent_id from jdw_membership where uid = {$row['user_id']}";
        $parent_id_result = $pdo->query($parent_id_sql);
        $parent_id = $parent_id_result->fetch(PDO::FETCH_ASSOC);
        if(!empty($parent_id['parent_id'])){
            //会员佣金
            $commission_sql = "select commission from jdw_shop where id = {$row['shop_id']}";
            $commission_result = $pdo->query($commission_sql);
            $commission = $commission_result->fetch(PDO::FETCH_ASSOC);

            if(empty($commission['commission'])){
                //平台统一设置回扣
                $system_commission_sql = "select value from jdw_config where id = 6";
                $system_commission_result = $pdo->query($system_commission_sql);
                $system_commission = $system_commission_result->fetch(PDO::FETCH_ASSOC);
                $system_commission = $system_commission['value'];
                
                if(!empty($system_commission)){
                  $system_commission_money = $row['total_price'] * ($system_commission/100);
                  $system_commission_money = substr(sprintf("%.3f",$system_commission_money),0,-1);
                  $row['total_price'] = $row['total_price'] - $system_commission_money;
                  $member_balance = $system_commission_money;
                }
                $member_commission = $system_commission;
            }else{
                //会员回扣
                $system_commission_money = $row['total_price'] * ($commission['commission']/100);
                $system_commission_money = substr(sprintf("%.3f",$system_commission_money),0,-1);
                $row['total_price'] = $row['total_price'] - $system_commission_money;
                $member_balance = $system_commission_money;
                $member_commission = $commission['commission'];
            }

            if(!empty($member_commission)){
                //平台记录
                $member_balance_sql = "insert into jdw_member_balance (uid,shop_id,money,order_id,commission_record) values ('{$parent_id['parent_id']}','{$row['shop_id']}','{$member_balance}','{$row['id']}','{$member_commission}')";
                $pdo->query($member_balance_sql);
            }
        }

        //商家返金额
        $mention_sql = "select money,status,id from jdw_mention_balance where order_id = {$row['id']}";
        $mention_result = $pdo->query($mention_sql);
        $mention_balanc_result = $mention_result->fetch(PDO::FETCH_ASSOC);

        if($mention_balanc_result['status'] == '0' && !empty($mention_balanc_result['id'])){
            if(!empty($mention_balanc_result['money'])){
                $updateMentionSql = "update jdw_mention_balance set status=1 where order_id = {$row['id']}";
                $pdo->query($updateMentionSql);
            }else{
                $mention_balanc_result['money'] = 0;
            }
        }else{
            $mention_balanc_result['money'] = 0;
        }

        $time = time();
        //资金流水
        $strSql = "insert into jdw_order_stream (order_id,shop_id,money,mention_money,creatime) values ('{$row['id']}','{$row['shop_id']}','{$row['total_price']}','{$mention_balanc_result['money']}',{$time})";
        $pdo->query($strSql);

        //添加返利资金
        if(!empty($mention_balanc_result['money'])){
            $updateSql = "update jdw_shop set balance=balance+{$mention_balanc_result['money']} where id={$row['shop_id']}";
            $pdo->query($updateSql);
        } 

        //更改商户零钱
        $updateSql = "update jdw_shop set balance=balance+{$row['total_price']} where id={$row['shop_id']}";
        $pdo->query($updateSql);

        //更改订单状态
        $updateOrderSql = "update jdw_order set status='3' where id={$row['id']}";
        $pdo->query($updateOrderSql);
        }
  }
?>